package com.qf.controller;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 创建验证码
 * 改一些代码, 演示git
 * @author 千锋健哥
 */
@WebServlet("/createCode")
public class CreateCodeController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 创建验证码对象,
        //第一参数: 验证码图片宽度, 第二参数:高度, 第三参数:验证码字符个数, 第四参数: 验证码所在行长度
        ValidateCode validateCode = new ValidateCode(200, 30, 4, 10);
        //2. 生成验证码字符串内容
        String code = validateCode.getCode();
        //3. 将验证码字符串存入session会话对象中保存
        HttpSession session = req.getSession();
        session.setAttribute("code", code);
        //4. 通过流将验证码图片写回到页面展示
        validateCode.write(resp.getOutputStream());
    }

}
